package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Visit;
import com.bonuo.crm.service.VisitService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//拜访记录Controller
@RestController
@RequestMapping("/crm/visit")
@Api(tags = "拜访记录管理")
public class VisitController extends BaseController {
    @Autowired
    private VisitService visitService;

    //查询拜访记录
    @GetMapping("/list")
    @ApiOperation(value = "查询拜访记录",httpMethod = "GET")
    public TableDataInfo list(Visit visit,Integer pageNum,Integer pageSize) {
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<Visit> visitList = visitService.selectVisitList(visit);
        return getDataTable(visitList);
    }

    //插入拜访记录
    @PostMapping("/add")
    @ApiOperation(value = "添加拜访记录",httpMethod = "POST")
    public AjaxResult add(@RequestBody Visit visit){
        Integer add = visitService.insertVisit(visit);
        return toAjax(add);
    }

    //删除拜访记录
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除拜访记录",httpMethod = "DELETE")
    public AjaxResult delete(@PathVariable Integer id){
        Integer delete = visitService.deleteVisitById(id);
        return toAjax(delete);
    }

    //修改拜访记录
    @PutMapping("/update")
    @ApiOperation(value = "修改拜访记录",httpMethod = "PUT")
    public AjaxResult update(@RequestBody Visit visit){
        Integer update = visitService.updateVisit(visit);
        return toAjax(update);
    }

    //导出拜访记录
    @PostMapping("/export")
    @ApiOperation(value = "导出拜访记录",httpMethod = "POST")
    public void export(HttpServletResponse response, Visit visit) {
        List<Visit> list = visitService.selectVisitList(visit);
        ExcelUtil<Visit> util = new ExcelUtil<Visit>(Visit.class);
        util.exportExcel(response, list, "拜访记录数据");
    }
}
